<script lang="ts">
import { defineComponent } from "vue";

export default defineComponent({
  props: {
    modelValue: { type: Number, required: true },
    min: { type: Number, default: 1 },
    max: { type: Number, default: 99999 },
    step: { type: Number, default: 1 },
    disabled: Boolean,
    theme: { type: String, default: "square" }, // circle
  },
  emits: {
    "update:modelValue": (value: number) => typeof value === "number",
  },
  setup(props, ctx) {
    const subClick = () => {
      if (props.disabled || props.modelValue === props.min) return;
      ctx.emit("update:modelValue", props.modelValue - props.step);
    };
    const addClick = () => {
      if (props.disabled || props.modelValue === props.max) return;
      ctx.emit("update:modelValue", props.modelValue + props.step);
    };
    return { subClick, addClick };
  },
});
</script>

<template>
  <view class="form-stepper">
    <template v-if="theme === 'square'">
      <view
        class="form-stepper-sub"
        :style="{ opacity: disabled || modelValue === min ? '0.5' : '1' }"
        @click="subClick"
      >
        <view class="form-stepper-horizontal"></view>
      </view>
      <view class="form-stepper-input">{{ modelValue }}</view>
      <view
        class="form-stepper-add"
        :style="{ opacity: disabled || modelValue === max ? '0.5' : '1' }"
        @click="addClick"
      >
        <view class="form-stepper-horizontal"></view>
        <view class="form-stepper-vertical"></view>
      </view>
    </template>

    <template v-else>
      <view
        class="form-stepper-circle"
        :style="{ opacity: disabled || modelValue === min ? '0.5' : '1' }"
        @click="subClick"
      >
        <view class="form-stepper-horizontal2"></view>
      </view>
      <view class="form-stepper-value">{{ modelValue }}</view>
      <view
        class="form-stepper-circle"
        :style="{ opacity: disabled || modelValue === max ? '0.5' : '1' }"
        @click="addClick"
      >
        <view class="form-stepper-horizontal2"></view>
        <view class="form-stepper-vertical2"></view>
      </view>
    </template>
  </view>
</template>

<style lang="scss">
.form-stepper {
  display: flex;
  align-items: center;
  flex-shrink: 0;

  .form-stepper-sub,
  .form-stepper-add {
    position: relative;
    width: 56rpx;
    height: 56rpx;
    border-radius: 8rpx 0 0 8rpx;
    background-color: #f2f3f5;
  }

  .form-stepper-horizontal,
  .form-stepper-vertical,
  .form-stepper-horizontal2,
  .form-stepper-vertical2 {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 24rpx;
    height: 2rpx;
    border-radius: 2rpx;
    background-color: #323233;
    transform: translate(-50%, -50%);
  }

  .form-stepper-input {
    width: 64rpx;
    margin: 0 4rpx;
    font-size: 30rpx;
    color: #333333;
    line-height: 56rpx;
    text-align: center;
    background-color: #f2f3f5;
  }

  .form-stepper-vertical,
  .form-stepper-vertical2 {
    width: 2rpx;
    height: 24rpx;
  }

  .form-stepper-add {
    border-radius: 0 8rpx 8rpx 0;
  }

  .form-stepper-circle {
    position: relative;
    width: 36rpx;
    height: 36rpx;
    border-radius: 50%;
    border: 2rpx solid #ff7900;
  }

  .form-stepper-horizontal2,
  .form-stepper-vertical2 {
    background-color: #ff7900;
  }

  .form-stepper-horizontal2 {
    width: 18rpx;
  }

  .form-stepper-vertical2 {
    height: 18rpx;
  }

  .form-stepper-value {
    width: 66rpx;
    font-size: 28rpx;
    color: #333333;
    line-height: 40rpx;
    text-align: center;
    font-weight: bold;
  }
}
</style>